package com.wangnian.wangcai.util;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.wangnian.wangcai.dao.DayInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Http工具类
 * Created by wangnian on 2016/6/2.
 */
@Component
public class HttpUtil {

    @Value("${qmt.http}")
    private String qmtHttp;

    private static String qmtHttpStr;

    @PostConstruct
    public void init() {
        qmtHttpStr = qmtHttp;
    }

    /**
     * get请求
     *
     * @param url
     * @return
     */
    public static String get(String url) {
        HttpRequest httpRequest = HttpRequest.get(url);
        return httpRequest.execute().charset("utf-8").body();
    }


    /**
     * 带参数的get请求
     *
     * @param url
     * @param parMap
     * @return
     */
    public static String get(String url, Map<String, Object> parMap) {
        HttpRequest httpRequest = HttpRequest.get(url);
        httpRequest.form(parMap);
        return httpRequest.execute().body();
    }

    /**
     * 带参数的Header请求的get请求
     *
     * @param url
     * @param parMap
     * @return
     */
    public static String get(String url, Map<String, Object> parMap, Map<String, String> headerMap) {
        HttpRequest httpRequest = HttpRequest.get(url);
        httpRequest.form(parMap);
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            httpRequest.header(entry.getKey(), entry.getValue());
        }
        return httpRequest.execute().body();
    }

    /**
     * 带参数post请求
     *
     * @param url
     * @return
     */
    public static String post(String url, Map<String, Object> parMap) {
        HttpRequest httpRequest = HttpRequest.post(url);
        httpRequest.form(parMap);
        return httpRequest.execute().body();
    }

    /**
     * RAW Post请求
     *
     * @param url
     * @param Body
     * @return
     */
    public static String post(String url, String Body) {
        HttpRequest httpRequest = HttpRequest.post(url);
        httpRequest.body(Body, "application/json");
        return httpRequest.execute().body();
    }


    /**
     * 获取qmt的数据
     *
     * @param codeList
     */
    public static List<DayInfo> getQmt(Set<String> codeList) {
        List<DayInfo> dayInfoList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        for (String code : codeList) {
            String first = code.substring(0, 1);
            if (first.equals("6") || first.equals("5")) {
                stringBuilder.append(code).append(".SH").append(",");
            } else {
                stringBuilder.append(code).append(".SZ").append(",");
            }
        }
        stringBuilder.append("000001.SH,");
        String codeStr = stringBuilder.toString();
        HttpRequest httpRequest = HttpRequest.get(qmtHttpStr + "get_full_tick?code_list=" + codeStr);
        String str = httpRequest.execute().body();
        JSONArray jsonArray = JSONArray.parse(str);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            DayInfo dayInfo = new DayInfo();
            String name = jsonObject.getString("name");
            if (name.equals("000001.SH")) {
                dayInfo.setCode("1.000001");
            } else {
                dayInfo.setCode(name.substring(0, name.indexOf(".")));
            }
            dayInfo.setTime(DateUtil.parseToDate(jsonObject.getString("timetag"), DateUtil.YYYYMMDD));
            dayInfo.setOpen(jsonObject.getDouble("open"));
            dayInfo.setClose(jsonObject.getDouble("lastPrice"));
            dayInfo.setHigh(jsonObject.getDouble("high"));
            dayInfo.setLow(jsonObject.getDouble("low"));
            dayInfo.setVolume(jsonObject.getDouble("volume"));
            dayInfo.setMoney(jsonObject.getDouble("amount"));
            dayInfo.setZf(Indicators.zf(dayInfo.getClose(), jsonObject.getDouble("lastClose")));
            dayInfoList.add(dayInfo);
        }
        return dayInfoList;
    }

    /**
     * 获取新浪的数据
     *
     * @param codeList
     */
    public static List<DayInfo> getSinaData(Set<String> codeList) {
        List<DayInfo> dayInfoList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        for (String code : codeList) {
            String first = code.substring(0, 1);
            if (first.equals("6") || first.equals("5")) {
                stringBuilder.append("sh").append(code).append(",");
            } else {
                stringBuilder.append("sz").append(code).append(",");
            }
        }
        stringBuilder.append("sh000001,");
        String codeStr = stringBuilder.toString();
        HttpRequest httpRequest = HttpRequest.get("http://hq.sinajs.cn/?rn=1555566862550&list=" + codeStr);
        httpRequest.header("Referer", "http://finance.sina.com.cn/");
        String str = httpRequest.execute().body();
        String[] resultInfos = str.split(";");
        String[] chars = codeStr.split(",");
        //1 是今天开盘  2 是昨天收盘 3 当前价格 4 最高 5 最低 8 量 9 交易额 30 日期
        for (int i = 0; i < chars.length; i++) {
            String code = chars[i].substring(2);
            String[] abcs = resultInfos[i].split(",");
            if (abcs.length < 5) {
                continue;
            }
            DayInfo dayInfo = new DayInfo();
            dayInfo.setCode(code);
            dayInfo.setTime(DateUtil.parseToDate(abcs[30], DateUtil.DATEFORMAT_YYYYMMDD));
            dayInfo.setOpen(Double.parseDouble(abcs[1]));
            dayInfo.setClose(Double.parseDouble(abcs[3]));
            dayInfo.setHigh(Double.parseDouble(abcs[4]));
            dayInfo.setLow(Double.parseDouble(abcs[5]));
            dayInfo.setVolume(Double.parseDouble(abcs[8]));
            dayInfo.setMoney(Double.parseDouble(abcs[9]));
            dayInfo.setZf(Indicators.zf(dayInfo.getClose(), Double.parseDouble(abcs[2])));
            dayInfoList.add(dayInfo);
        }
        return dayInfoList;
    }
}
